package com.company.manage.system.service;

import com.company.manage.common.base.BaseService;
import com.company.manage.common.persistence.HibernateDao;
import com.company.manage.common.utils.Exceptions;
import com.company.manage.system.dao.PermissionDao;
import com.company.manage.system.entity.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HuangYong on 2016/1/23.
 * desc:
 */
@Service
@Transactional(readOnly = true)
public class PermissionService extends BaseService<Permission, Integer> {
    private static final String TAG = "PermissionService";

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public HibernateDao<Permission, Integer> getEntityDao() {
        return permissionDao;
    }

    /**
     * 添加基础操作
     *
     * @param pid        菜单id
     * @param pClassName 菜单权限标识名称
     */
    @Transactional(readOnly = false)
    public void addBaseOperation(Integer pid, String pClassName) {
        List<Permission> pList = new ArrayList<Permission>();

        pList.add(new Permission(pid, "添加", "O", "", "sys:" + pClassName + ":add"));
        pList.add(new Permission(pid, "删除", "O", "", "sys:" + pClassName + ":delete"));
        pList.add(new Permission(pid, "修改", "O", "", "sys:" + pClassName + ":update"));
        pList.add(new Permission(pid, "查看", "O", "", "sys:" + pClassName + ":view"));

        /*添加没有的基本操作*/
        List<Permission> existPList = getMenuOperation(pid);
        for (Permission permission : pList) {
            boolean exist = false;
            for (Permission existP : existPList) {
                if (permission.getPermCode().equals(existP.getPermCode())) {
                    exist = true;
                    break;
                } else {
                    exist = false;
                }
                if (!exist) {
                    save(permission);
                }
            }
        }
    }

    /**
     * 获取菜单下的操作
     *
     * @param pid
     * @return
     */
    public List<Permission> getMenuOperation(Integer pid) {
        return permissionDao.findMenuOperation(pid);
    }

    /**
     * 根据用户id获取角色拥有的权限名称
     *
     * @param userId
     * @return
     */
    public List<Permission> getPermissions(Integer userId) {
        try {
            List<Permission> permissions = permissionDao.findPermissions(userId);
            return permissions;
        } catch (Exception e) {
            e.printStackTrace();
            throw Exceptions.unchecked(e);
        }
    }

    /**
     * 获取角色拥有的菜单
     *
     * @param userId
     * @return
     */
    public List<Permission> getMenus(Integer userId) {
        return permissionDao.findMenus(userId);
    }

    /**
     * 获取所有菜单
     *
     * @return
     */
    public List<Permission> getMenus() {
        return permissionDao.findMenus();
    }
}